package com.dinadi.yidi.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.dinadi.yidi.R;
import com.dinadi.yidi.dialog.DialogControl;
import com.dinadi.yidi.dialog.DialogHelp;
import com.dinadi.yidi.dialog.MyProgressDialog;
import com.dinadi.yidi.ui.MyApplication;
import com.dinadi.yidi.utils.TLog;
import com.dinadi.yidi.utils.app.AppManager;
import com.umeng.message.PushAgent;

import butterknife.ButterKnife;


/**
 * Created by hc-3020-i3 on 2016/7/20.
 */
public abstract class BaseFragment extends Fragment implements DialogControl, View.OnClickListener {
    public boolean isTitle = true;
    private ProgressDialog _waitDialog;
    /*弹框是否显示*/
    private boolean _isVisible = true;
    public LoadingPage mLoadingPage;


    //表示控件初始化是否完成
    protected boolean isInit = false;
    private boolean isLoad = false;
    private MyProgressDialog _loadingDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mLoadingPage == null) {
            mLoadingPage = new LoadingPage(getContext().getApplicationContext()) {

                @Override
                public View createSuccessView() {
                    return getLayout();
                }
            };
            initView();
            initListener();
            registerCommonBtn();
            // isInit = true;
            // lazyLoad();
            initData();
            PushAgent.getInstance(getActivity()).onAppStart();
        }
        return mLoadingPage;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     * 判断Fragment的UI是否可见，实现Fragment数据的懒加载
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        lazyLoad();
    }

    /**
     * 显示一个内容为msg的吐司
     */
    public void toast(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示一个内容为msgId引用的String的吐司
     */
    public void toast(int msgId) {
        Toast.makeText(getActivity(), msgId, Toast.LENGTH_SHORT).show();
    }

    /**
     * 改变显示的页面
     * public final int STATE_LOADING = 0;//加载中的状态
     * public final int STATE_ERROR = 1;//加载失败的状态
     * public final int STATE_SUCCESS = 2;//加载成功的状态
     */
    public void changePager(int state) {
        if (mLoadingPage != null) {
            mLoadingPage.mState = state;
            mLoadingPage.showPage();
        }
    }

    protected void lazyLoad() {
        if (!isInit) {
            return;
        }
        if (getUserVisibleHint()) {
            isLoad = true;
            initData();
        }
    }

    @Override
    public void onDestroyView() {
        TLog.error(getClass().getName() + "");
        super.onDestroyView();
        isInit = false;
        isLoad = false;
        if (mLoadingPage != null) {
            ((ViewGroup) mLoadingPage.getParent()).removeView(mLoadingPage);
        }
    }

    /**
     * 设置错误页的标题,图片等
     *
     * @param tv_error_title
     */
    public void setErTitle(String tv_error_title, int iv_error, boolean isHasBtn) {
        mLoadingPage.setErrorTitle(tv_error_title, iv_error, isHasBtn);
    }

    /**
     * 设置是否显示标题栏 true 显示 false 部显示
     */
    protected void setIsTitle(boolean isTitle) {
        this.isTitle = isTitle;
    }


    /**
     * 加载框的显示和移除
     *
     * @param messege
     */
    public void showLoadingDialog(String messege) {
        if (_loadingDialog == null) {
            _loadingDialog = new MyProgressDialog(getActivity(), R.style.progress_dialog);
        }
        _loadingDialog.openProgressDialog(messege);
    }

    public void closeLoadingDialog() {
        if (_loadingDialog != null) {
            _loadingDialog.closeProgressDialog();
        }
    }


    //以下方法为网络请求时的等待框
    @Override
    public ProgressDialog showWaitDialog() {
        return showWaitDialog(R.string.loading);
    }

    @Override
    public ProgressDialog showWaitDialog(int resid) {
        return showWaitDialog(getString(resid));
    }

    @Override
    public ProgressDialog showWaitDialog(String message) {
        if (_isVisible) {
            if (_waitDialog == null) {
                _waitDialog = DialogHelp.getWaitDialog(getActivity(), message);
            }
            if (_waitDialog != null) {
                _waitDialog.setMessage(message);
                _waitDialog.show();
            }
            return _waitDialog;
        }
        return null;
    }

    @Override
    public void hideWaitDialog() {
        if (_isVisible && _waitDialog != null) {
            try {
                _waitDialog.dismiss();
                _waitDialog = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }


    /**
     * 在多个界面间公用的按钮，在此处注册点击事件
     */
    private void registerCommonBtn() {
        //返回按钮
        View view = mLoadingPage.findViewById(R.id.title_back);
        //lodding 返回按钮

        if (view != null) {
            view.setOnClickListener(this);

        }

        //重新加载按钮
        ImageView bt_error = (ImageView) mLoadingPage.findViewById(R.id.btn_reload);
//        TextView bt_error = (TextView)findViewById(R.id.bt_error);

        if (bt_error != null) {
            bt_error.setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_reload:
                noWifiRefresh();
                //错误按钮
                break;
            default:
                clickEvent(view);
                break;
        }

    }


    /**
     * 没有网络的情况下显示没有网络的界面，点击重新刷新
     */
    protected abstract void noWifiRefresh();

    /**
     * 返回当前Activity使用的布局对象
     */
    protected abstract View getLayout();

    /**
     * 只执行findViewById操作
     */
    protected abstract void initView();

    /**
     * 注册监听器，适配器
     */
    protected abstract void initListener();


    protected abstract void clickEvent(View v);

    /**
     * 数据加载，采用的是懒加载的模式
     */
    protected abstract void initData();

    protected void registerClickListener(View... views) {
        for (View v : views) {
            v.setOnClickListener(this);
        }
    }


}
